package com.js.data.dtmg.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.data.common.api.CommonResult;
import com.js.data.dtmg.dto.DtDatasourceDto;
import com.js.data.dtmg.dto.DtSourceQuery;
import com.js.data.dtmg.entity.DtDatasource;
import com.js.data.dtmg.service.DtDatasourceService;
import com.js.data.dtmg.vo.DtDatasourceVO;
import com.js.data.system.annotation.Log;
import com.js.data.system.query.UserQuery;
import com.js.data.system.service.UserService;
import com.js.data.system.vo.UserPageVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据源表 前端控制器
 * </p>
 *
 * @author hh
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/dtmg/dtDatasource")
public class DtDatasourceController {

    @Resource
    private DtDatasourceService service;

    /**
     * 用户分页
     *
     * @param query 查询条件
     * @return IPage<UserPageVO>
     */
    @GetMapping(value = "pageList")
    public CommonResult<IPage<DtDatasourceVO>> pageList(@ModelAttribute DtSourceQuery query) {
        return CommonResult.success(service.pageList(query));
    }
    @GetMapping(value = "selectList")
    public CommonResult<List<DtDatasource>> selectList() {
        return CommonResult.success(service.selectList());
    }
    /**
     * 新增
     *
     * @param dto 新增
     * @return void
     */
    @Log(value = "新增数据源")
    @PostMapping(value = "/save")
    public CommonResult<Void> save(@Validated @RequestBody DtDatasourceDto dto) {
        service.save(dto);
        return CommonResult.success();
    }
    /**
     * 查询数据源详情
     *
     * @param id 数据源id
     * @return DtDatasourceVO
     */
    @GetMapping(value = "/detail/{id}")
    public CommonResult<DtDatasourceVO> detail(@PathVariable Long id) {
        return CommonResult.success(service.detail(id));
    }
    @GetMapping(value = "/dbTables/{id}")
    public CommonResult<List<Map<String,String>>> dbTables(@PathVariable Long id) {
        return CommonResult.success(service.getDbTables(id));
    }
    /**
     * 删除数据源
     *
     * @param id 数据源id
     * @return void
     */
    @Log(value = "删除数据源")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return CommonResult.success();
    }

    @PostMapping(value = "/test")
    public CommonResult<Void> test(@Validated @RequestBody DtDatasourceDto dto) {
        service.testOk(dto);
        return CommonResult.success();
    }
}

